package dao

import (
	"time"

	"gorm.io/gorm"
)

var (
	DB *gorm.DB
)

type UserDao struct {
	Id          int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	NickName    string    `gorm:"column:nick_name;type:varchar(20);NOT NULL" json:"nick_name"`
	Email       string    `gorm:"column:email;type:varchar(100);NOT NULL" json:"email"`
	Passwd      string    `gorm:"column:passwd;type:varchar(200);NOT NULL" json:"passwd"`
	CreatedTime time.Time `gorm:"column:created_time;type:datetime;NOT NULL" json:"created_time"`
	UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;NOT NULL" json:"updated_time"`
}

func (d *UserDao) Model() *gorm.DB {
	return DB.Table("user")
}

func (d *UserDao) FindById(id int) (UserDao, error) {
	out := UserDao{}
	tx := d.Model()
	tx = tx.Where("id = ?", id)
	if err := tx.Find(&out).Error; err != nil {
		return out, err
	}
	return out, nil
}
